
#include <stdio.h>
#include <stdlib.h>
#include "tagfolder.h"
#include "tf_log.h"
#include <unistd.h>

void show_help(void);

int main( int argc, char** argv){
    ENTER_FUNC;
    tf_init( argc, argv);

    int c;
    extern char* optarg;
    extern int optind;
    string_list* p=NULL;
    int i;

    while( -1 != ( c= getopt( argc, argv, "d:l::r:vt:af"))){
        switch( c){
            case 'a':
                list_all_tags( &p);
                if( p!=NULL){
                    for( i=0; i< p->length; ++i){
                        printf( "Tag: %s\n", get_from_string_list( p, i));
                    }
                }
                free_string_list( p);
                break;
            case 'f':
                list_all_files( &p);
                if( p!=NULL){
                    for( i=0; i<p->length; ++i){
                        printf( "Tagged file: %s\n", get_from_string_list(p,i));
                    }
                }
                free_string_list(p);
                break;
            case 'd':
                if(0== define_tag( optarg)){
                    printf( "Tag %s defined\n", optarg);
                }
                break;
            case 'l':
                if( argv[optind]!=NULL && argv[optind+1]!=NULL && (0== tag_file ( argv[optind], argv[optind+1]))){
                    printf( "Tag %s linked to file %s\n", argv[optind], argv[optind+1]);
                }else{
                    printf("%s_%d error: failed to link %s\n", __func__, __LINE__, argv[optind]);
                }
                break;
            case 'r':
                if( 0==undefine_tag ( optarg)){
                    printf("Tag %s removed\n", optarg);
                }
                break;
            case 't':
                if( 0==if_tag_defined( optarg)){
                    printf( "Tag %s does not exist\n", optarg);
                }else{
                    printf( "Tag %s exists\n", optarg);
                }
                break;
            default:
                show_help();
        }
    }
    tf_destroy();
    LEAVE_FUNC;
    return 0;
}

void show_help(){
    ENTER_FUNC;
    printf("No help available\n");
    LEAVE_FUNC;
}
